from .models import *
from . import elineStat, eTeachOption, eAdminOption
import datetime
from .commAPI import apiObj

class cDBtoDo():
    def __init__(self):
        pass

    '''
        函数名:queryModelType
        描述：获取章节类型列表
        参数：enable:是否可用标志取值范围:elineStat
        返回值：list 章节模板列表,json格式
        '''

    def queryModelType(self, enable=None):
        mtlist = None
        if enable is None or enable == elineStat.all:
            mtlist = db.session.query(tModelType.id, tModelType.name, tModelType.createtime, tModelType.flag).all()
        else:
            mtlist = db.session.query(tModelType.id, tModelType.name, tModelType.createtime, tModelType.flag).filter(
                tModelType.flag == enable)
        itemlist = list()

        for x in mtlist:
            qdata = dict()
            qdata['id'] = x[0]
            qdata['name'] = x[1]
            qdata['createtime'] = str(x[2])
            qdata['flag'] = x[3]
            itemlist.append(qdata)
        return itemlist

    '''
    函数名:queryCaptionModel
    描述：获取章节模板列表
    参数：enable:是否可用标志取值范围:elineStat
    返回值：list 章节模板列表,json格式
    '''
    def queryCaptionModel(self, enable=None):
        mtlist=None
        if enable is None or enable == elineStat.all:
            mtlist = db.session.query(tCaptionmodel.id, tCaptionmodel.name, tCaptionmodel.disc, tCaptionmodel.modelfile,
                                      tCaptionmodel.modeldata,
                                      tCaptionmodel.createtime,
                                      tCaptionmodel.modeftime, tModelType.name, tCaptionmodel.flag).filter(
                tCaptionmodel.modeltype == tModelType.id)
        else:
            mtlist = db.session.query(tCaptionmodel.id, tCaptionmodel.name, tCaptionmodel.disc, tCaptionmodel.modelfile,
                                      tCaptionmodel.modeldata,
                                      tCaptionmodel.createtime,
                                      tCaptionmodel.modeftime, tModelType.name, tCaptionmodel.flag).filter(
                tCaptionmodel.modeltype == tModelType.id).filter(tCaptionmodel.flag == enable)
        itemlist = list()

        for x in mtlist:
            qdata = dict()
            qdata['id'] = x[0]
            qdata['name'] = x[1]
            qdata['disc'] = x[2]
            qdata['file'] = x[3]
            qdata['data'] = x[4]
            qdata['createtime'] = str(x[5])
            qdata['modeftime'] = str(x[6])
            qdata['type'] = x[7]
            qdata['flag'] = x[8]
            itemlist.append(qdata)
        return itemlist


    '''
    函数名:querySchool
    描述：获取学校列表
    参数：无
    返回值：list 章节模板列表,json格式
    '''
    def querySchool(self):

        schlist = tSchool.query.first()
        itemlist = list()
        if schlist is not None:
            qdata = dict()
            qdata['id'] = schlist.id
            qdata['name'] = schlist.name
            qdata['addr'] = schlist.addr
            qdata['phone'] = schlist.phone
            qdata['fox'] = schlist.fox
            qdata['homepage'] = schlist.homepage
            qdata['logo'] = schlist.logo
            qdata['disc'] = schlist.disc
            itemlist.append(qdata)
        return itemlist


    '''
    函数名:School
    描述：获取学院列表
    参数：enable:是否可用标志取值范围:elineStat
    返回值：list 学 院列表,json格式
    '''
    def queryCollege(self,enable=None):
        items = list()
        if enable is None or enable == elineStat.all:
            collegelist = db.session.query(tCollege.id, tCollege.name, tCollege.createtime, tCollege.flag,
                                           tCollege.name).filter(tCollege.id == tCollege.schoolid)
        else:
            collegelist = db.session.query(tCollege.id, tCollege.name, tCollege.createtime,
                                           tCollege.flag,
                                           tSchool.name).filter(tSchool.id == tCollege.schoolid).filter(
                tCollege.flag == enable)
        for x in collegelist:
            qdata = dict()
            qdata['id'] = x[0]
            qdata['name'] = x[1]
            qdata['createtime'] = str(x[2])
            qdata['schoolname'] = x[4]
            qdata['flag'] = x[3]
            items.append(qdata)
        return items
    '''
    函数名:queryMajor
    描述：获取专业列表
    参数：enable:是否可用标志取值范围:elineStat
    返回值：list 专业列表,json格式
    '''
    def queryMajor(self,enable=None):
        if enable == None or enable == elineStat.all:
            majorlist = db.session.query(tMajor.id, tMajor.name, tMajor.createtime, tMajor.flag,
                                         tCollege.name, tMajor.collegeid).filter(tCollege.id == tMajor.collegeid)
        else:
            majorlist = db.session.query(tMajor.id, tMajor.name, tMajor.createtime, tMajor.flag,
                                         tCollege.name, tMajor.collegeid).filter(
                tCollege.id == tMajor.collegeid).filter(tMajor.flag == enable)
        item = list()
        for x in majorlist:
            qdata = dict()
            qdata['id'] = x[0]
            qdata['name'] = x[1]
            qdata['createtime'] = str(x[2])
            qdata['college'] = x[4]
            qdata['collegeid'] = x[5]
            qdata['flag'] = x[3]
            item.append(qdata)
        return item
    '''
    函数名:queryClazz
    描述：获取班级列表
    参数：enable:是否可用标志取值范围:elineStat
    返回值：list 班级列表,json格式
    '''
    def queryClazz(self,enable=None):
        if enable == None or enable == elineStat.all:
            clazzlist = db.session.query(tClazz.id, tClazz.name, tClazz.createtime, tClazz.flag,
                                         tMajor.name, tClazz.majorid).filter(
                tMajor.id == tClazz.majorid)
        else:
            clazzlist = db.session.query(tClazz.id, tClazz.name, tClazz.createtime, tClazz.flag,
                                         tMajor.name, tClazz.majorid).filter(
                tMajor.id == tClazz.majorid).filter(tClazz.flag == enable)
        items = list()
        for x in clazzlist:
            qdata = dict()
            qdata['id'] = x[0]
            qdata['name'] = x[1]
            qdata['createtime'] = str(x[2])
            qdata['major'] = x[4]
            qdata['majorid'] = x[5]
            qdata['flag'] = x[3]
            items.append(qdata)
        return items
    '''
        函数名:queryPath
        描述：获取学习路径列表
        参数：enable:是否可用标志取值范围:elineStat
        返回值：list 班级列表,json格式
        '''
    def queryPath(self, enable=None):
        if enable == None or enable == elineStat.all:
            pathList = db.session.query(tPath.id, tPath.name, tPath.createtime, tPath.modiftime, tPath.flag).all()
        else:
            pathList = db.session.query(tPath.id, tPath.name, tPath.createtime, tPath.modiftime, tPath.flag).filter(
                tPath.flag == enable)
        items = list()
        for x in pathList:
            qdata = dict()
            qdata['id'] = x[0]
            qdata['name'] = x[1]
            qdata['createtime'] = str(x[2])
            qdata['modiftime'] = str(x[3])
            qdata['flag'] = x[4]
            items.append(qdata)
        return items
    '''
        函数名:queryPathStep
        描述：获取学习路径阶段列表
        参数：enable:是否可用标志取值范围:elineStat
        返回值：list 班级列表,json格式
        '''
    def queryPathStep(self, enable=None):
        if enable == None or enable == elineStat.all:
            pathSetpList = db.session.query(tPathStep.id, tPathStep.name, tPathStep.createtime, tPathStep.level,
                                            tPathStep.pathid, tPath.name, tPathStep.flag).filter(
                tPathStep.pathid == tPath.id)
        else:
            pathSetpList = db.session.query(tPathStep.id, tPathStep.name, tPathStep.createtime, tPathStep.level,
                                            tPathStep.pathid, tPath.name, tPathStep.flag).filter(
                tPathStep.pathid == tPath.id).filter(tPathStep.flag == enable)
        items = list()
        for x in pathSetpList:
            qdata = dict()
            qdata['id'] = x[0]
            qdata['name'] = x[1]
            qdata['createtime'] = str(x[2])
            qdata['disc'] = str(x[3])
            qdata['flag'] = x[4]
            items.append(qdata)
        return items

    '''
        函数名:queryCourse
        描述：获取课程列表
        参数：enable:是否可用标志取值范围:elineStat
        返回值：list 班级列表,json格式
        '''
    def queryCourse(self, enable=None):
        if enable == None or enable == elineStat.all:
            itemlist = db.session.query(tCourse.id, tCourse.name, tCourse.createtime, tCourse.modiftime, tCourse.disc,
                                        tCourse.flag).all()
        else:
            itemlist = db.session.query(tCourse.id, tCourse.name, tCourse.createtime, tCourse.modiftime, tCourse.disc,
                                        tCourse.flag).filter(tCourse.flag == enable)
        items = list()
        for x in itemlist:
            qdata = dict()
            qdata['id'] = x[0]
            qdata['name'] = x[1]
            qdata['createtime'] = str(x[2])
            qdata['modiftime'] = str(x[3])
            qdata['disc'] = str(x[4])
            qdata['flag'] = x[5]
            qdata['oldID'] = x[6]
            items.append(qdata)
        return items

    '''
        函数名:queryCourseByID
        描述：通过id获取课程列表
        参数：id：课程id
        返回值：list 班级列表,json格式
        '''

    def queryCourseByID(self, id):
        itemlist = db.session.query(tCourse.id, tCourse.name, tCourse.createtime, tCourse.modiftime, tCourse.disc,
                                    tCourse.flag).filter(id == id).first()
        return itemlist


    '''
        函数名:queryChapter
        描述：获取章节列表
        参数：enable:是否可用标志取值范围:elineStat
        返回值：list 班级列表,json格式
        '''
    def queryChapter(self, enable=None):
        if enable == None or enable == elineStat.all:
            itemlist = db.session.query(tCaption.id, tCaption.name, tCaption.disc,
                                        tCaption.createtime, tCaption.modeftime, tCaption.modelid,
                                        tCaption.courseid, tCourse.name, tCaption.level, tCaption.flag,
                                        tCaption.teahoption).fliter(
                tCourse.id == tCaption.courseid)
        else:
            itemlist = db.session.query(tCaption.id, tCaption.name, tCaption.disc,
                                        tCaption.createtime, tCaption.modeftime, tCaption.modelid,
                                        tCaption.courseid, tCourse.name, tCaption.level, tCaption.flag,
                                        tCaption.teahoption).filter(
                tCaption.courseid == tCourse.id).filter(tCaption.flag == enable)
        items = list()
        for x in itemlist:
            qdata = dict()
            qdata['id'] = x[0]
            qdata['name'] = x[1]
            qdata['disc'] = x[2]
            qdata['createtime'] = str(x[3])
            qdata['modiftime'] = str(x[4])
            qdata['modelname'] = x[5]
            qdata['courseid'] = x[6]
            qdata['coursename'] = x[7]
            qdata['level'] = x[8]
            qdata['flag'] = x[9]
            qdata['option'] = x[10]
            items.append(qdata)
        return items

    '''
        函数名:queryChapterByID
        描述：添加章节
        参数：id:章节id
        返回值：数据集
        '''

    def queryChapterByID(self, id):
        itemlist = db.session.query(tCaption.id, tCaption.name, tCaption.disc,
                                    tCaption.createtime, tCaption.modeftime, tCaption.modelid,
                                    tCaption.courseid, tCaption.level, tCaption.flag, tCaption.teahoption).fliter(
            tCaption.id == id)
        return itemlist

    '''
        函数名:addChapter
        描述：添加章节
        参数：name:章节名称,disc:章节描述，courseid：课程id，modelid：模板id，level：章节顺序
        返回值：list 班级列表,json格式
        '''

    def addChapter(self, name, courseid, disc, modelid, level):
        id = apiObj.createGUID()
        newChapter = tCaption(id=id, name=name, disc=disc, modelid=modelid, courseid=courseid, level=level,
                              createtime=datetime.datetime.now(), modeftime=datetime.datetime.now(),
                              flag=elineStat.examine, option=eTeachOption.add)

        db.session.add(newChapter)
        db.session.commit()
        return id

    '''
        函数名:queryChapterByID
        描述：通过获取章节列表
        参数：id:章节id
        返回值：list数据库对象
        '''

    def queryChapterByID(self, id):
        itemlist = db.session.query(tCaption.id, tCaption.name, tCaption.disc,
                                    tCaption.createtime, tCaption.modeftime, tCaption.modelid,
                                    tCaption.courseid, tCaption.level, tCaption.flag).filter(tCaption.id == id).first()
        return itemlist

    '''
        函数名:queryChapterByCourseID
        描述：通过课程id获取章节列表
        参数：id:章节id,
        返回值：list数据库对象
        '''

    def queryChapterByCourseID(self, id):
        itemlist = db.session.query(tCaption.id, tCaption.name, tCaption.disc,
                                    tCaption.createtime, tCaption.modeftime, tCaption.modelid,
                                    tCaption.courseid, tCaption.level, tCaption.flag).filter(tCaption.courseid == id)
        return itemlist

    '''
        函数名:setFlagChapter
        描述：设置指定章节但可用标识
        参数：id:章节id,flag:标识
        返回值：章节id
        '''

    def setFlagChapter(self, id, flag):
        ret = tCaption.query.filter_by(id=id).first()
        if ret is not None:
            ret.flag = flag
            db.session.add(ret)
            db.session.commit()
        return ret.id

    '''
        函数名:setCourseIDChapter
        描述：设置可用章节的课程id
        参数：id:章节id,flag:标识
        返回值：章节id
        '''

    def setCourseIDChapter(self, id, courseid):
        ret = tCaption.query.filter_by(id=id).first()
        if ret is not None:
            ret.courseid = courseid
            db.session.add(ret)
            db.session.commit()
        return ret.id

    '''
        函数名:queryRole
        描述：获取角色列表
        参数：enable:是否可用标志取值范围:elineStat
        返回值：list 班级列表,json格式
        '''
    def queryRole(self, enable=None):
        if enable == None or enable == elineStat.all:
            roleList = db.session.query(tRole.id, tRole.name, tRole.createtime, tRole.flag).all()
        else:
            roleList = db.session.query(tRole.id, tRole.name, tRole.createtime, tRole.flag).filter(tRole.flag == enable)
        items = list()
        for x in roleList:
            qdata = dict()
            qdata['id'] = x[0]
            qdata['name'] = x[1]
            qdata['createtime'] = str(x[2])
            qdata['flag'] = x[3]
            items.append(qdata)
        return items

    '''
        函数名:queryAllUser
        描述：获取用户列表
        参数：enable:是否可用标志取值范围:elineStat
        返回值：list 班级列表,json格式
        '''

    def queryAllUser(self, enable=None):
        if enable == None or enable == elineStat.all:
            userList = db.session.query(tAllUser.id, tAllUser.name, tAllUser.createtime, tRole.name,
                                        tAllUser.flag).filter(
                tAllUser.roleid == tRole.id)
        else:
            userList = db.session.query(tAllUser.id, tAllUser.name, tAllUser.createtime, tRole.name,
                                        tAllUser.flag).filter(
                tAllUser.roleid == tRole.id).filter(tAllUser.flag == enable)
        items = list()
        for x in userList:
            qdata = dict()
            qdata['id'] = x[0]
            qdata['name'] = x[1]
            qdata['createtime'] = str(x[2])
            qdata['role'] = x[3]
            qdata['flag'] = x[4]
            items.append(qdata)
        return items

    '''
        函数名:queryMatch
        描述：获取比赛列表
        参数：enable:是否可用标志取值范围:elineStat
        返回值：list 班级列表,json格式
        '''

    def queryMatch(self, enable=None):
        if enable == None or enable == elineStat.all:
            itemlist = db.session.query(tMatch.id, tMatch.name, tMatch.createteacherid, tAllUser.name, tMatch.modelsid,
                                        tCaptionmodel.name, tCaptionmodel.modelfile, tCaptionmodel.modeldata,
                                        tMatch.signtime,
                                        tMatch.createtime, tMatch.startime, tMatch.endtime, tMatch.studcount,
                                        tMatch.judgescount, tMatch.flag).filter(tMatch.createtime == tAllUser.id
                                                                                and tCaptionmodel.id == tMatch.modelsid)
        else:
            itemlist = db.session.query(tMatch.id, tMatch.name, tMatch.createteacherid, tMatch.modelsid,
                                        tCaption.name, tCaptionmodel.modelfile, tCaptionmodel.modeldata,
                                        tMatch.signtime,
                                        tMatch.createtime, tMatch.startime, tMatch.endtime, tMatch.studcount,
                                        tMatch.judgescount, tMatch.flag).filter(tMatch.createteacherid == tAllUser.id
                                                                                and tCaptionmodel.id == tMatch.modelsid).filter(
                tMatch.flag == enable)
        items = list()
        for x in itemlist:
            qdata = dict()
            qdata['id'] = x[0]
            qdata['name'] = x[1]
            qdata['creatteacherid'] = x[2]
            qdata['creatteachername'] = x[3]
            qdata['creatmodelsid'] = x[4]
            qdata['createmodelsname'] = x[5]
            qdata['file'] = x[6]
            qdata['data'] = x[7]
            qdata['signtime'] = x[8]
            qdata['createtime'] = str(x[9])
            qdata['startime'] = x[10]
            qdata['endtime'] = x[11]
            qdata['studcount'] = x[12]
            qdata['judgecount'] = x[13]
            qdata['flag'] = x[14]
            items.append(qdata)
        return items

    '''
        函数名:queryPlayer
        描述：获取选手列表
        参数：enable:是否可用标志取值范围:elineStat
        返回值：list 班级列表,json格式
        '''

    def queryPlayer(self, matchid=None, enable=None):
        if enable == None or enable == elineStat.all:
            itemlist = db.session.query(tPlayer.id, tPlayer.matchid, tMatch.name, tPlayer.alluserid, tAllUser.name,
                                        tPlayer.createtime, tPlayer.flag).filter(tPlayer.matchid == tMatch.id and
                                                                                 tPlayer.alluserid == tAllUser.id)
        else:
            itemlist = db.session.query(tPlayer.id, tPlayer.matchid, tMatch.name, tPlayer.alluserid, tAllUser.name,
                                        tPlayer.createtime, tPlayer.flag).filter(tPlayer.matchid == tMatch.id and
                                                                                 tPlayer.alluserid == tAllUser.id).filter(
                tPlayer.flag == enable)
        if matchid is not None:
            itemlist = itemlist.filter(tPlayer.matchid == matchid)
        items = list()
        for x in itemlist:
            qdata = dict()
            qdata['id'] = x[0]
            qdata['matchid'] = x[1]
            qdata['matchname'] = x[2]
            qdata['playerid'] = str(x[3])
            qdata['playername'] = x[4]
            qdata['createtime'] = x[5]
            qdata['flag'] = x[6]
            items.append(qdata)
        return items

    '''
        函数名:queryJudger
        描述：获取裁判列表
        参数：enable:是否可用标志取值范围:elineStat
        返回值：list 班级列表,json格式
        '''
    def queryJudger(self, matchid=None, enable=None):

        itemlist = db.session.query(tJudger.id, tJudger.matchid, tMatch.name, tJudger.alluserid, tAllUser.name,
                                    tJudger.createtime, tJudger.flag).filter(tPlayer.matchid == tMatch.id and
                                                                             tPlayer.alluserid == tAllUser.id)

        if matchid is not None:
            itemlist = itemlist.filter(tPlayer.matchid == matchid)
        if enable is not None or enable != elineStat.all():
            itemlist.filter(tJudger.flag == enable)
        items = list()
        for x in itemlist:
            qdata = dict()
            qdata['id'] = x[0]
            qdata['matchid'] = x[1]
            qdata['matchname'] = x[2]
            qdata['judgerid'] = str(x[3])
            qdata['judgername'] = x[4]
            qdata['createtime'] = x[5]
            qdata['flag'] = x[6]
            items.append(qdata)
        return items

    # 行政关系类表操作-------------------------------------------------------------
    # 直接操作数据表，修改不天加新记录
    # 添加/修改学生-班级对应关系
    '''
        函数名:addRelation_Stu2Class
        描述：获取裁判列表
        参数：stuId:学生,clazzId:班级id,option:执行操作取值范围eDBOption
        返回值:成功id，失败none
        '''

    def addRelation_Stu2Class(self, stuid, clazzid):
        stu = tAllUser.query.filter_by(id=stuid)
        clazzes = tClazz.query.filter_by(id=clazzid)
        relation = tMap_studclazz.query.filter_by(stuid=stuid, clazzid=clazzid)
        id = None
        if stu is not None and clazzes is not None:

            if relation is None:
                id = apiObj.createGUID()
                newRelation = tMap_studclazz(id=id, stuid=stuid, clazzid=clazzid)
                db.session.add(newRelation)
            else:
                id = relation.id
                relation.stuid = stuid
                relation.clazzid = clazzid
                db.session.add(relation)
            db.session.commit()
        else:
            return None
        return id

    '''
        函数名:delRelation_Stu2Class
        描述：删除学生-班级对应关系
        参数：id:主键
        返回值:True or False
        '''

    def delRelation_Stu2Class(self, id):
        relation = tMap_studclazz.query.filter_by(id=id)
        if relation is None:
            return True
        else:
            relation.flag = elineStat.disable
            db.session.add(relation)
            db.session.commit()
            return True
        return True

    '''
        函数名:modefRelation_Stu2Class_Flag
        描述：修改学生-班级对应关系-标志字段
        参数：id:主键, flag标志位：取值范围elineStat
        返回值:True or False
        '''

    def modefRelation_Stu2Class_Flag(self, id, flag):
        relation = tMap_studclazz.query.filter_by(id=id)
        if relation is None:
            return True
        else:
            relation.flag = flag
            db.session.add(relation)
            db.session.commit()
            return True
        return True

    '''
        函数名:addRelation_Teach2ClassCourse
        描述：添加老师-班级对应关系
        参数：teachid:老师,clazzid:班级id,courseid:课程
        返回值:True or False
        '''

    def addRelation_Teach2ClassCourse(self, teachid, clazzid, courseid):
        teach = tAllUser.query.filter_by(id=teachid)
        clazzes = tClazz.query.filter_by(id=clazzid)
        course = tCourse.query.filter_by(id=courseid)
        relation = tMap_teachCourseClazz.query.filter_by(stuid=teach, clazzid=clazzid, courseid=courseid)
        if teach is not None and clazzes is not None and courseid is not None:
            if relation is None:
                id = apiObj.createGUID()
                newRelation = tMap_teachCourseClazz(id=id, stuid=teach, clazzid=clazzid, courseid=courseid)
                db.session.add(newRelation)
            else:
                id = relation.id
                relation.stuid = teach
                relation.clazzid = clazzid
                relation.clazzid = courseid
                db.session.add(relation)
            db.session.commit()
        else:
            return None
        return id

    '''
        函数名:delRelation_Teach2ClassCourse
        描述：删除老师-班级对应关系
        参数：id:主键
        返回值:True or False
        '''

    def delRelation_Teach2ClassCourse(self, id):
        relation = tMap_teachCourseClazz.query.filter_by(id=id)
        if relation is None:
            return True
        else:
            relation.flag = elineStat.disable
            db.session.add(relation)
            db.session.commit()
            return True
        return True

    '''
        函数名:modefRelation_Teach2ClassCourse_Flag
        描述：修改老师-班级对应关系-标志字段
        参数：id:主键, flag标志位：取值范围elineStat
        返回值:True or False
        '''

    def modefRelation_Teach2ClassCourse_Flag(self, id, flag):
        relation = tMap_teachCourseClazz.query.filter_by(id=id)
        if relation is None:
            return True
        else:
            relation.flag = flag
            db.session.add(relation)
            db.session.commit()
            return True
        return True

    # '''
    #     函数名:addRelastion_PathStep2Path
    #     描述：添加路径阶段-学习路径对应关系
    #     参数：pathid:学习路径id,pathstepid:路径阶段id
    #     返回值:True or False
    #     '''
    #
    # def addRelastion_PathStep2Path(self, pathid, pathstepid, level):
    #     pathid = tPath.query.filter_by(id=pathid)
    #     stepid = tClazz.query.filter_by(id=pathstepid)
    #     relation = tMap_PathPathStep.query.filter_by(pathid=pathid, pathstepid=pathstepid)
    #     if pathid is not None and stepid is not None:
    #         newRelation = tMap_teach(pathid=pathid, pathstepid=pathstepid, createtime=datetime.now(),
    #                                  modiftime=datetime.now(), level=level)
    #         db.session.add(newRelation)
    #         db.session.commit()
    #     return True
    #
    # '''
    #     函数名:delRelation_PathStep2Path
    #     描述：删除路径阶段-学习路径对应关系
    #     参数：id:主键
    #     返回值:True or False
    #     '''
    #
    # def delRelation_PathStep2Path(self, id):
    #     relation = tMap_PathPathStep.query.filter_by(id=id)
    #     if relation is None:
    #         return True
    #     else:
    #         relation.flag = elineStat.disable
    #         db.session.add(relation)
    #         db.session.commit()
    #         return True
    #     return True
    #
    # '''
    #     函数名:modefRelation_PathStep2Path_Flag
    #     描述：删除路径阶段-学习路径对应关系
    #     参数：id:主键
    #     返回值:True or False
    #     '''
    #
    # def modefRelation_PathStep2Path_Flag(self, id, flag):
    #     relation = tMap_PathPathStep.query.filter_by(id=id)
    #     if relation is None:
    #         return True
    #     else:
    #         relation.flag = flag
    #         db.session.add(relation)
    #         db.session.commit()
    #         return True
    #     return True

    '''
        函数名:addRelation_Course2PathStep
        描述：添加课程到路径阶段
        参数：courseid:课程id,stepid：学习阶段id
        返回值:成功id，失败none
        '''

    def addRelation_Course2PathStep(self, courseid, stepid):
        course = tCourse.query.filter_by(id=courseid)
        step = tPathStep.query.filter_by(id=stepid)

        if course is not None and step is not None:
            id = apiObj.createGUID()
            newRelation = tMap_Course2PathStep(id=id, courseid=courseid, pathstepid=stepid)
            db.session.add(newRelation)
            db.session.commit()
            return id
        else:
            return None


    '''
        函数名:delRelation_Course2PathStep
        描述：添加课程到路径阶段
        参数：id主键
        返回值:True or False
        '''

    def delRelation_Course2PathStep(self, id):
        relation = tMap_Course2PathStep.query.filter_by(id=id)
        if relation is None:
            return True
        else:
            relation.flag = elineStat.disable
            db.session.add(relation)
            db.session.commit()
            return True
        return True

    '''
        函数名:modefRelation_Course2PathStep_Flag
        描述：添加课程到路径阶段
        参数：id主键,标志字段flag，取值范围elineStat
        返回值:True or False
        '''

    def modefRelation_Course2PathStep_Flag(self, id, flag):
        relation = tMap_Course2PathStep.query.filter_by(id=id)
        if relation is None:
            return True
        else:
            if flag == elineStat.enable:  # 同意操作
                # 关闭已存在但关系

                relation.flag = flag
                db.session.add(relation)
                db.session.commit()
            return True
        return True

    '''
        函数名:queryLabel
        描述：获取标签
        参数：标志字段flag，取值范围elineStat
        返回值：结果集 json格式
        '''

    def queryLabel(self, flag):
        if flag is None or flag == elineStat.all:
            retlist = db.session.query(tLabel.id, tLabel.name, tLabel.teahoption, tLabel.createtime, tLabel.flag).all()
        else:
            retlist = db.session.query(tLabel.id, tLabel.name, tLabel.teahoption, tLabel.createtime,
                                       tLabel.flag).filter(tLabel.flag == flag)

        items = list()
        for x in retlist:
            qdata = dict()
            qdata['id'] = x[0]
            qdata['name'] = x[1]
            qdata['option'] = x[2]
            qdata['createtime'] = x[3]
            qdata['flag'] = x[6]
            items.append(qdata)
        return items

    '''
        函数名:addLabel
        描述：添加标签
        参数：name:标签名称
        返回值:成功id，失败none
        '''

    def addLabel(self, name):
        ret = tLabel.query.filter_by(name=name).first()
        if ret is None:
            id = apiObj.createGUID(name)
            newRelation = tLabel(id=id, name=name, createtime=datetime.datetime.now(), flag=elineStat.examine,
                                 option=eTeachOption.add)
            db.session.add(newRelation)
        else:
            id = ret.id
            ret.name = name
            ret.createtime = datetime.datetime.now()
            ret.flag = elineStat.examine
            ret.teahoption = eTeachOption
            db.session.add(ret)
        db.session.commit()
        return id

    '''
        函数名:delLabel
        描述：删除标签
        参数：id:标签id
        返回值：True or False
        '''

    def delLabel(self, id):
        ret = tLabel.query.filter_by(id=id)
        if ret is None:
            return False
        else:
            db.session.delete(ret)
            db.session.commit()

    '''
        函数名:addPath
        描述：添加路径
        参数：name:路径名称,disc：路径描述, logo：路径logo
        返回值:成功id，失败none
        '''

    def addRelation_CourseLabel(self, courseID, labelID):
        id = apiObj.createGUID()
        newRet = tMap_CourseLabel(id=id, courseid=courseID, labelid=labelID,
                                  createtime=datetime.datetime.now(), flag=elineStat.enable, option=eTeachOption.add)
        db.session.add(newRet)
        db.session.commit()
        return id

    '''
        函数名:addPath
        描述：添加路径
        参数：name:路径名称,disc：路径描述, logo：路径logo
        返回值:成功id，失败none
        '''

    def addPath(self, name, disc, logo):
        id = apiObj.createGUID(name)
        newRet = tPath(id=id, name=name, disc=disc, logo=logo,
                       createtime=datetime.datetime.now(), modiftime=datetime.datetime.now(), option=eTeachOption.add,
                       flag=elineStat.examine)
        db.session.add(newRet)
        db.session.commit()
        return id

    '''
        函数名:delPath
        描述：删除路径
        参数：id:路径id
        返回值 True or False
        '''

    def delPath(self, id):
        ret = tPath.query.filter_by(id=id).first()
        if ret is not None:
            ret.flag = elineStat.disable
            ret.teahoption = eTeachOption.delete
            db.session.add(ret)
            db.session.commit()
        return True

    '''
        函数名:setflagPath
        描述：删除路径，老师用
        参数：id:路径id,flag:设置标志
        返回值 True or False
        '''

    def setflagPath(self, id, flag):
        ret = tPath.query.filter_by(id=id).first()
        if ret is not None:
            ret.flag = flag
            db.session.add(ret)
            db.session.commit()
        return True

    '''
        函数名:addPathStep
        描述：删除标签
        参数：id:标签id
        返回值:成功id，失败none
        '''

    def addPathStep(self, name, level, pathid):
        id = apiObj.createGUID(name)
        newRet = tPathStep(id=id, name=name, level=level, pathid=pathid,
                           createtime=datetime.datetime.now(), modiftime=datetime.datetime.now(),
                           option=eTeachOption.add,
                           flag=elineStat.examine)
        db.session.add(newRet)
        db.session.commit()
        return id

    '''
            函数名:delPathStep
            描述：删除路径阶段
            参数：id:标签id
            返回值
            '''

    def delPathStep(self, id):
        ret = tPathStep.query.filter_by(id=id).first()
        if ret is not None:
            ret.flag = elineStat.examine
            ret.teahoption = eTeachOption.delete
            db.session.add(ret)
            db.session.commit()
        return True

    '''
        函数名:setFlagPathStep
        描述：修改标志位
        参数：id:阶段id, flag:标识
        返回值
        '''

    def setFlagPathStep(self, id, flag):
        ret = tPathStep.query.filter_by(id=id).first()
        if ret is not None:
            ret.flag = flag
            db.session.add(ret)
            db.session.commit()
        return True

    '''
        函数名:addCourse
        描述：添加课程
        参数：name:课程名称，disc：课程描述，logo：课程图片,oldID:旧课程
        返回值:成功id，失败none
        '''

    def addCourse(self, name, disc, logo, oldID=None):
        id = apiObj.createGUID(name)
        newRet = tCourse(id=id, name=name, disc=disc, logo=logo, createtime=datetime.datetime.now(),
                         modiftime=datetime.datetime.now(), flag=elineStat.examine, option=eTeachOption.add,
                         oldID=oldID)
        db.session.add(newRet)
        db.session.commit()
        return id

    '''
        函数名:delCourse
        描述：删除课程
        参数：id:阶段id,
        返回值 None
        '''

    def delCourse(self, id):
        ret = tCourse.query.filter_by(id=id)
        if ret is not None:
            ret.flag = elineStat.examine
            ret.teahoption = eTeachOption.delete
            db.session.add(ret)
            db.session.commit()
        return True

    '''
        函数名:setFlagCourse
        描述：修改课程标识
        参数：id:阶段id,flag：标识
        返回值 True or False
        '''

    def setFlagCourse(self, id, flag):
        ret = tCourse.query.filter_by(id=id).first()
        if ret is not None:
            ret.flag = flag
            db.session.add(ret)
            db.session.commit()
        return True
